/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2008 www.pretty-tools.com
 */

var wsshSaveImageHelper = {
    logger: yrprxFactory.createLogger("wsshSaveImageHelper"),
    baseName: "", // prefix for generated files
    suffix: 0 // suffix for generated files
};

wsshSaveImageHelper.getPad2Suffix = function ()
{
    if (this.suffix < 10)
        return "0" + (this.suffix++);
    else
        return this.suffix++;
}

wsshSaveImageHelper.getDefaultFileName = function (src)
{
    var defaultFileName = yrprxUtil.getFileNameWithExtensionFromPath(src);

    if (defaultFileName.indexOf('?') != -1 || defaultFileName.indexOf('&') != -1)
        defaultFileName = "";

    if (defaultFileName == "")
    {
        if (this.suffix == 0)
        {
            this.baseName = new String(new Date().getTime());
            if (this.baseName.length > 7) // trim file name
                this.baseName = this.baseName.substring(this.baseName.length-7);
        }
        defaultFileName = this.baseName + this.getPad2Suffix();
    }

    return defaultFileName;
}

wsshSaveImageHelper.saveImage = function (src)
{
    try
    {
        if (navigator.userAgent.indexOf("Windows NT 5") == -1) //TODO: currently we don't support save on VISTA
            throw "Currently we don't support save on VISTA!";

        if (this.logger.enabled) this.logger.debug("saveImage: " + src);
        if (!src || !src.length)
            throw "Invalid image URL!";

        this.suffix = 0; // reset suffix
        var defaultFileName = this.getDefaultFileName(src);
        var ext = yrprxUtil.getExtension(defaultFileName);
        if (ext == "") // we cannot get extension but JPG is the most popular
            defaultFileName += ".jpg";

        var fp = yrprxComponentsFactory.createFilePicker();
        // init file picker
        fp.init(window, wsshResources["webslideshow.filepicker.save.image"], Components.interfaces.nsIFilePicker.modeSave);
        fp.appendFilter(wsshResources["webslideshow.filepicker.filter.all"], "*.*");
        fp.defaultString = defaultFileName;
        fp.defaultExtension = ext;
        // open file picker
        if (fp.show() == Components.interfaces.nsIFilePicker.returnCancel)
            return;

        yrprxComponentsFactory.createWebBrowserPersist().saveURI(src, fp.file);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("saveImage: ", ex);
    }
}

wsshSaveImageHelper.saveAllImages = function (sources)
{
    try
    {
        if (navigator.userAgent.indexOf("Windows NT 5") == -1) //TODO: currently we don't support save on VISTA
            throw "Currently we don't support save on VISTA!";

        if (!sources || !sources.length)
            throw "Nothing to save!";

        var fp = yrprxComponentsFactory.createFilePicker();
        // init file picker
        fp.init(window, wsshResources["webslideshow.filepicker.save.all.images"], Components.interfaces.nsIFilePicker.modeGetFolder);
        // open file picker
        if (fp.show() == Components.interfaces.nsIFilePicker.returnCancel)
            return;

        this.suffix = 0; // reset suffix
        wsshWindowManager.openSaveImagesProgressDialog(sources, fp.file);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("saveAllImages: ", ex);
    }
}

wsshSaveImageHelper.getImageFileName = function (destFolder, src)
{
    var defaultFileName = this.getDefaultFileName(src);
    if (yrprxUtil.getExtension(defaultFileName) == "")
        defaultFileName += ".jpg";

    var file = yrprxComponentsFactory.createLocalFile();
    file.initWithFile(destFolder);
    file.append(defaultFileName);
    return file;
}
